package cn.alex.sso.project.controller;

import cn.alex.sso.framework.annotations.OperatLog;
import cn.alex.sso.framework.constant.ActionConstant;
import cn.alex.sso.framework.constant.ModuleConstant;
import cn.alex.sso.framework.core.Result;
import cn.alex.sso.project.entity.SysUser;
import cn.alex.sso.project.entity.params.EditSysUserPassword;
import cn.alex.sso.project.entity.params.EditSysUserStatus;
import cn.alex.sso.project.service.SysUserService;
import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户
 *
 * @author Alex
 * @date 2024/9/28 22:26
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class SysUserController {
    private final SysUserService baseService;

    /**
     * 获取分页列表
     */
    @GetMapping("/getPage")
    public Result getPage(SysUser entity) {
        return Result.ok(baseService.getPage(entity));
    }

    /**
     * 获取登录用户信息
     */
    @GetMapping("/getLoginInfo")
    public Result getLoginInfo() {
        long loginUserId = StpUtil.getLoginIdAsLong();
        SysUser user = baseService.getById(loginUserId);

        Map<String, Object> result = new HashMap<>();
        result.put("user", user);
        return Result.ok(result);
    }

    /**
     * 获取详情
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Long id) {
        return Result.ok(baseService.getInfo(id));
    }

    /**
     * 新增
     */
    @PostMapping
    @OperatLog(module = ModuleConstant.USER, action = ActionConstant.ADD)
    public Result add(@RequestBody SysUser entity) {
        baseService.add(entity);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @OperatLog(module = ModuleConstant.USER, action = ActionConstant.EDIT)
    public Result edit(@RequestBody SysUser entity) {
        baseService.edit(entity);
        return Result.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @OperatLog(module = ModuleConstant.USER, action = ActionConstant.REMOVE)
    public Result del(@RequestBody List<Long> ids) {
        baseService.del(ids);
        return Result.ok();
    }

    /**
     * 修改状态
     */
    @PostMapping("/editStatus")
    @OperatLog(module = ModuleConstant.USER, action = ActionConstant.EDIT + "状态")
    public Result editStatus(@RequestBody EditSysUserStatus editSysUserStatus) {
        baseService.editStatus(editSysUserStatus);
        return Result.ok();
    }

    /**
     * 修改密码
     */
    @PostMapping("/editPassword")
    @OperatLog(module = ModuleConstant.USER, action = ActionConstant.EDIT + "密码")
    public Result editPassword(@RequestBody EditSysUserPassword editSysUserPassword) {
        baseService.editPassWord(editSysUserPassword);
        return Result.ok();
    }
}
